<template>
<view class="container">
<view :style="topheight + ';background: #fff;'"></view>
<!-- 自定义头部 -->
<view class="head">
  <view @tap="toOrder">
    <image src="/static/remao_shop/img/arrow_left.png"></image>
  </view>
  <view>支付</view>
  <image></image>
</view>
  <!-- 余額 -->
  <view class="money">
    <view style="font-size:26rpx;color:#a1a1a1;text-align:center">支付金额</view>
    <view style="font-size:44rpx;text-align:center">￥{{payMoney}}</view>
  </view>
  <!-- 支付方式 -->
  <view class="payType">
    <radio-group class="radio-group" @change="radioChange">
      <label class="radio">
        <view class="payweixin">
          <image src="/static/remao_shop/img/weixinpay.png"></image>
          <view>微信支付</view>
          <radio value="0" checked="true"></radio>
        </view>
      </label>
      <!-- <label class="radio">
        <view class='payyue'>
          <image src='../../img/yu-e.png'></image>
          <view>余额支付</view>
          <radio value="1" />
        </view>
      </label> -->
    </radio-group>
  </view>
  <!-- 确认支付 -->
  <view @tap="commitpay" class="conmitpay">确认支付</view>
</view>
</template>

<script>
import api from "../../../api";
var app = getApp();

export default {
  data() {
    return {
      userId: '',
      openId: '',
      // 支付方式
      paytype: '0',
      // 订单编号
      order: '',
      // 支付类型
      type: '',
      // 支付金额
      payMoney: '',
      // 自定义导航头部适配
      topheight: ''
    };
  },

  components: {},
  props: {},

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    var that = this;
    console.log(options);
    this.setData({
      order: options.order,
      type: options.type,
      payMoney: options.money,
      userId: options.userId
    });
    console.log(that.userId);
    wx.getStorage({
      key: 'openid',
      success: function (res) {
        console.log(res);
        that.setData({
          openId: res.data
        });
      }
    }); // 自定义导航适配

    var num = wx.getSystemInfoSync()['statusBarHeight'];
    console.log(num);
    this.setData({
      topheight: 'height:' + num + 'px'
    });
  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {},

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {},

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {},

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {},

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {},

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {},

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {},
  methods: {
    radioChange: function (e) {
      this.setData({
        paytype: e.detail.value
      });
      console.log(this.paytype);
    },
    // 确认支付
    commitpay: function () {
      var _that = this; // console.log(_that.data.order)
      // console.log(_that.data.type)
      // console.log(_that.data.payMoney)


      wx.request({
        url: api.fyz.weixinPay,
        data: {
          type: _that.type,
          pay_sn: _that.order,
          money: _that.payMoney,
          openid: _that.openId,
          w_type: 'JSAPI'
        },
        header: {
          'content-type': 'application/x-www-form-urlencoded' // 默认值

        },
        method: 'POST',
        success: res => {
          console.log(res); //  付款成功

          if (res.data.status == 1) {
            wx.requestPayment({
              appId: res.data.weixin_config.appid,
              nonceStr: res.data.weixin_config.nonce_str,
              package: 'prepay_id=' + res.data.weixin_config.prepay_id,
              signType: 'MD5',
              timeStamp: res.data.weixin_config.timestamp,
              paySign: res.data.weixin_config.sign,
              success: function (res1) {
                //  更新订单状态
                wx.request({
                  url: api.fyz.updataOrder,
                  data: {
                    user_id: _that.userId,
                    order_sn: _that.order,
                    payment_id: 2
                  },
                  header: {
                    'content-type': 'application/x-www-form-urlencoded' // 默认值

                  },
                  method: 'POST',
                  success: res => {
                    console.log(res);

                    if (res.data.status == 1) {
                      console.log(_that.userId);
                      wx.redirectTo({
                        url: '../../packageB/order/order?status=0&userId=' + _that.userId
                      });
                    } else {
                      wx.showToast({
                        title: res.data.msg,
                        duration: 1000
                      });
                    }
                  }
                });
              },
              fail: function (res2) {
                wx.showToast({
                  title: '支付失败，请重新支付',
                  duration: 1000
                });
              }
            });
          }
        },
        fail: function (ret) {
          console.log(ret);
        },
        complete: function (reb) {
          console.log(reb);
        }
      });
    },
    // 跳转订单
    toOrder: function () {
      let that = this;
      wx.showModal({
        // title: '',
        content: '确认要离开吗？',

        success(res) {
          if (res.confirm) {
            console.log('用户点击确定');
            wx.redirectTo({
              url: '../../packageB/order/order?status=0&userId=' + that.userId
            });
          } else if (res.cancel) {
            console.log('用户点击取消');
            return;
          }
        }

      });
    }
  }
};
</script>
<style>
@import "./pay.css";
</style>